package org.millioncall.yueyoga.admin.action;

import hirondelle.date4j.DateTime;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.millioncall.yueyoga.admin.model.CardModel;
import org.millioncall.yueyoga.admin.model.ChargerModel;
import org.millioncall.yueyoga.admin.model.HallName;
import org.millioncall.yueyoga.admin.model.MonthConsult;
import org.millioncall.yueyoga.admin.service.CardReportsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.googlecode.aviator.runtime.function.system.Date2StringFunction;

@Controller
public class ReportsAction extends BaseAction {
	private CardReportsService cardReportsService;

	public CardReportsService getCardReportsService() {
		return cardReportsService;
	}

	public void setCardReportsService(CardReportsService cardReportsService) {
		this.cardReportsService = cardReportsService;
	}

	@RequestMapping(value = "/cardreports.jhtml")
	public String cardReports(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		super.initFrontHeader(request, model);
		Map<String, Object> map = new HashMap<String, Object>();
		String startdate1 = request.getParameter("startdate1");
		String startdate2 = request.getParameter("startdate2");
		if (StringUtils.isNotBlank(startdate1)) {
			map.put("startdate1", startdate1);
		}
		if (StringUtils.isNotBlank(startdate2)) {
			map.put("startdate2", startdate2);
		}
		if (StringUtils.isNotBlank(startdate1)
				&& StringUtils.isNotBlank(startdate2)
				&& startdate1.equals(startdate2)

		) {
			String da1 = " 00:00:00";
			String da2 = "23:59:59";
			String dat1 = startdate1 + da1;
			String dat2 = startdate2 + da2;
			map.put("startdate1", dat1);
			map.put("startdate2", dat2);
		}
		map.put("code", this.getUser(request).getOrganizationId());
		CardModel cm = this.cardReportsService.findCardCounts(map);
		HallName hallName = this.cardReportsService.hallName(this.getUser(
				request).getOrganizationId());
		model.put("cm", cm);
		model.put("hallName", hallName.getHallName());
		return "card_reports";
	}

	/** 查找负责人业绩 */
	@RequestMapping(value = "/findChargerReport.jhtml")
	public String chargerReports(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		String startdate = request.getParameter("startdate");
		String enddate = request.getParameter("enddate");

		Map<String, Object> map = new HashMap<String, Object>();

		if (!StringUtils.isBlank(startdate)) {
			map.put("startdate", startdate);
		}

		if (!StringUtils.isBlank(enddate)) {
			map.put("enddate", enddate);
		}

		map.put("hallcode", this.getUser(request).getOrganizationId());

		ChargerModel cm = this.cardReportsService.findChargerModel(map);

		model.put("cm", cm);
		model.put("startdate", startdate);
		model.put("enddate", enddate);

		return "chargerReport_list";
	}

	@RequestMapping(value = "/monthReports.jhtml")
	public String MonthReports(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		super.initFrontHeader(request, model);
		String startdate1 = request.getParameter("startdate1");
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(startdate1)) {
			map.put("startdate1", startdate1);
		}
		MonthConsult monthReports = this.cardReportsService
				.findMonthReport(map);
		HallName hallName = this.cardReportsService.hallName(this.getUser(
				request).getOrganizationId());
		model.put("hallName", hallName.getHallName());
		model.put("monthReports", monthReports);
		return "monthReports";
	}
}
